//
//  XMActivityViewController.m
//  XunMi
//
//  Created by MacBook on 2024/3/6.
//  Copyright © 2024 TieZhu. All rights reserved.
//

#import "XMActivityViewController.h"
#import "XMActivityTableViewCell.h"
#import "XMActivityModel.h"

@interface XMActivityViewController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray <XMActivityModel *> *dataArray;
@property (nonatomic, assign) NSInteger page;
@end

@implementation XMActivityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initVariables];
    [self setupSubviews];
    [self activityListData];
    [self setupRefresh];
}

#pragma mark - 初始化变量
- (void)initVariables {
    self.page = 1;
    self.dataArray = [NSMutableArray array];
}

#pragma mark - refresh
- (void)setupRefresh {
    XMWeakSelf(self);
    self.tableView.mj_header = [XMRefreshNormalHeader headerWithRefreshingBlock:^{
        XMStrongSelf(self);
        [self activityListDataRefresh];
    }];
    
    XMRefreshBackFooter *footer = [XMRefreshBackFooter footerWithRefreshingBlock:^{
        XMStrongSelf(self);
        self.page += 1;
        [self activityListData];
    }];
    self.tableView.mj_footer = footer;
    self.tableView.mj_footer.hidden = YES;
}

- (void)activityListDataRefresh {
    self.page = 1;
    [self.dataArray removeAllObjects];
    [self activityListData];
}

- (void)activityListData {
    [MBProgressHUD showHUDActivityMsg:nil toView:kWindow];
    NSMutableDictionary *dic = @{
        @"page_no": @(self.page),
        @"page_size": @(10),
    }.mutableCopy;
    
    [YHNetworkManager.sharedInstance postWithUrl:@"api/config/activityList" parameters:dic completeBlock:^(XMResponseTip * _Nonnull tip, id  _Nullable result) {
        [self.tableView hideStatusView];
        if (tip.success) {
            [MBProgressHUD hideHUD];
            NSArray *record = [result safe_objectForKey:@"record"];
            NSArray *array = [XMActivityModel mj_objectArrayWithKeyValuesArray:record];
            [self.dataArray addObjectsFromArray:array];
            if (self.dataArray.count == 0) {
                XMWeakSelf(self);
                [self.tableView showStatusView:(YHViewStatusTypeEmptyData)];
                self.tableView.statusTitleLabel.text = @"还没有活动哦～";
            }
        }
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        self.tableView.mj_footer.hidden = self.dataArray.count == 0;
        if (tip.loadMore) {
            [self.tableView.mj_footer endRefreshing];
        } else {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
    }];
}

#pragma mark - setupSubViews
- (void)setupSubviews {
    [self.view addSubview:self.tableView];
    [self.tableView registerNib:kNibWithName(kStringFromClass(XMActivityTableViewCell)) forCellReuseIdentifier:kStringFromClass(XMActivityTableViewCell)];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.tableView.frame = self.view.bounds;
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XMActivityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XMActivityTableViewCell"];
    if (self.dataArray.count > 0) {
        cell.model = self.dataArray[indexPath.row];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 262;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    XMActivityModel *model = self.dataArray[indexPath.row];
    if (![XMUtil isBlankString:model.link]) {
        XMWebViewController *vc = [[XMWebViewController alloc] init];
        vc.title = model.title;
        vc.urlStr = model.link;
        vc.needNav = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

#pragma mark - lazy
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            _tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
            _tableView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 0, 0);
        }
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = UIColor.whiteColor;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = YES;
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];
        // 解决在iOS11上朋友圈demo文字收折或者展开时出现cell跳动问题
        _tableView.estimatedRowHeight = CGFLOAT_MIN;
        _tableView.estimatedSectionFooterHeight = CGFLOAT_MIN;
        _tableView.estimatedSectionHeaderHeight = CGFLOAT_MIN;
        _tableView.rowHeight = UITableViewAutomaticDimension;
    }
    return _tableView;
}

#pragma mark - JXCategoryListContentViewDelegate
- (UIView *)listView {
    return self.view;
}

@end
